package com.cy7;

/** 二叉搜索树的实现
 * @author CY
 * @date 2022/10/24 08:33
 **/
public class MyBinarySearchTree {
    static class TreeNode {
        TreeNode left;
        TreeNode right;
        int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    TreeNode root;

    public boolean search(int val) {
        if (root == null) {
            return false;
        }
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val == val) {
                return true;
            }
            if (cur.val < val) {
                cur = cur.right;
            }
            if (cur.val > val) {
                cur = cur.left;
            }
        }
        return false;
    }

    //二叉搜索树的添加
    public boolean push(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val == val) {
                return false;
            } else if (cur.val > val) {
                parent = cur;
                cur = cur.left;
                if (cur == null) {
                    parent.left = new TreeNode(val);
                }
            } else {
                parent = cur;
                cur = cur.right;
                if (cur == null) {
                    parent.right = new TreeNode(val);
                }
            }
        }
        return true;
    }

    //二叉树搜索树的删除
    public TreeNode pop(int val) {
        if (root == null) {
            return null;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else {
                break;
            }
        }
        if (cur == null) {
            return null;
        }
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (parent.left == cur) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur==root) {
                root = cur.left;
            } else if (parent.right == cur) {
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }


        } else {
            TreeNode tmp = cur.right;
            TreeNode tmpD = cur;
            while (tmp.left != null) {
                tmpD = tmp;
                tmp = tmp.left;
            }
            cur.val = tmp.val;
            if (tmpD.left == tmp) {
                tmpD.left = tmp.right;
            }
            if (tmpD.left == null) {
                cur.right = tmpD.right;
            }

        }
        return root;
    }

    public void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }

    public static void main(String[] args) {
        MyBinarySearchTree myBinarySearchTree = new MyBinarySearchTree();
        int[] arr = {2, 5, 8, 4, 7, 9, 23};
        for (int i = 0; i < arr.length; i++) {
            myBinarySearchTree.push(arr[i]);
        }
        myBinarySearchTree.inorder(myBinarySearchTree.root);
        System.out.println();
        myBinarySearchTree.pop(2);
        myBinarySearchTree.inorder(myBinarySearchTree.root);

//        System.out.println();
//        myBinarySearchTree.inorder(myBinarySearchTree.root);
    }
}
